﻿function gcValue(interval){
	var mOffset=interval[0];
	var mLength=interval[1];
	var mTitle=(typeof(interval[2])!='undefined')?interval[2]:'';
	var K =429/86400;
	
	this.draw = function() {
		return '<div class="gcValue" style="position:absolute; left:'+(154+mOffset*K)+'px; width:'+(mLength*K)+'px;" title='+mTitle+'></div>'
	}
}

function gcValues() {
	var mValues=new Array();
	
	this.addValue=function(interval){
		if (!interval[1]>0) return undefined; // length
		mValues[mValues.length]=new gcValue(interval);
		return mValues[mValues.length-1];
	}
	this.draw=function() {
		values='';
		for (var i=0;i<mValues.length;i++)
			values=values+mValues[i].draw();
		return values
	}

}
function gcPoint(point) {
	var mId=point[0];
	var mName=point[1];
	var mIdx=point[2];
	var mValues=new gcValues();
	
	this.addValue = function(interval) { return mValues.addValue(interval); }
	this.getId = function() { return mId; }
	this.draw=function(){
		 var gcPoint='<div class="gcPoint" style="position:absolute;left:5px">'+mName+'</div>'
		 +mValues.draw();
		 return gcPoint
	}
}

function gcPoints() {
	var mPoints = new Array();
	
	this.count=function(){
		var points = mPoints.length;
		return points;
	}
	this.addPoint = function (point) {
		for (var i=0;i<mPoints.length;i++)
			if (mPoints[i].getId() == (new gcPoint(point)).getId())
				return mPoints[i];
		
		point[2]=mPoints.length-1;
		mPoints[mPoints.length]=new gcPoint(point);
		return mPoints[mPoints.length-1];
	}
	this.draw=function(){
		var points='';
		for (var i=0;i<mPoints.length;i++){
			points=points
				+'<div id="gcPoints" style="position:absolute; top:'+(50+26*i)+'px; width:600px; height=24px">'
				+ mPoints[i].draw()
				+'</div>'
		}
		return points;
	}
}

function GanttChart(el) {
	var mThis = el;
	var mPoints = new gcPoints();
	var pointHeigth = 30;
	var pointNameLength = 150;
	var bodyLength = 530;
	
	this.addPoint = function (point) { return mPoints.addPoint(point); }
	this.draw=function(){
		var table='<table border="1" style="border-collapse:collapse">';
		var tableH='<thead><tr><td rowspan="2" style="width:'+pointNameLength+'px;"><div class="GTaskTitle">Today</div></td><td class="GMonth" colspan="24">TODAY</td></tr><tr>';
		for (var i=0; i<24; i++) {
			tableH=tableH+'<td class="GDay"><div style="width:15px;">'+ (i) +'</div></td>';
		}
		tableH=tableH+'</tr></thead>';
		
		var tableB = '<tbody><tr><td style="height:'+(26*mPoints.count())+'"></td>';
		for (var i=0; i<24; i++) {
			tableB=tableB+'<td><div style="width:15px;"></div></td>';
		}
		tableB=tableB+'</tr></tbody>';
		
		table=table+tableH+tableB+'</table>';
		mThis.innerHTML=table+mPoints.draw();
	}
}